import time
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait

#定义一个basePage模块，在该模块下定义个BasePage类
#在该类中，初始化self.driver、
# （定义查找元素方法、向元素输入内容、点击元素的处理）均是移动过来的
#其他page类继承BasePage类
class BasePage():
    eloginOrUname=(By.XPATH,"//*[@id='ECS_MEMBERZONE']/a[1]")
    def __init__(self,driver=None):
        if driver is None:
            self.driver = webdriver.WebDriver()
            # self.driver.get('http://192.168.234.152/user.php')
            self.driver.get('http://192.168.234.152')
            self.driver.maximize_window()
        else:
            self.driver:WebDriver=driver

        self.driver.implicitly_wait(10)
        time.sleep(1)
    #查找元素的方法，，，需要传值过来  传查找方式
    def findEle(self,locator):
        try:
            print("开始查找元素~~~~~")
            ele:WebElement=WebDriverWait(self.driver,10,1).\
                until(lambda d:d.find_element(*locator))
        except Exception as e:
            print(f"查找元素发生异常{locator}，异常信息{e}")
        else:
            print(f"元素找到了{locator}")
            return ele
        finally:
            print(f"查找完成{locator}~~~")
    def inputText(self,ele,msg):
        try:
            print("向元素中输入内容")
            ele.send_keys(msg)
        except Exception as e:
            print(f"向元素中输入内容发生异常{ele}，异常信息{e}")
        else:
            print(f"向元素中输入内容成功{ele}")
        finally:
            print(f"向元素中输入内容完成~~~")

    #对元素点击处理
    def eleClick(self,ele):
        try:
            print("点击元素~~~~")
            ele.click()
        except Exception as e:
            print(f"点击元素发生异常{ele}，异常信息{e}")
        else:
            print(f"点击元素成功{ele}")
        finally:
            print(f"点击元素完成~~~~~")

    def getELeTxt(self,ele):
        # ele=self.findEle(ele)
        try:
            print("获取元素的文本~~~~")
            t=ele.text
        except Exception as e:
            print(f"获取元素的文本发生异常{ele}，异常信息{e}")
        else:
            print(f"获取元素的文本成功{ele}")
            return t
        finally:
            print(f"获取元素的文本完成~~~~~")

    def getAlertTxt(self):
        try:
            print("获取弹窗的文本~~~~")
            alert: Alert = self.driver.switch_to.alert
            t = alert.text
            alert.accept()
        except Exception as e:
            print(f"获取弹窗的文本发生异常，异常信息{e}")
        else:
            print(f"获取弹窗的文本成功")
            return t
        finally:
            print(f"获取弹窗的文本完成~~~~~")

    def pageClose(self):
        self.driver.quit()